﻿//#define SINGLE_INSTANCE

using Buddy;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace $safeprojectname$
{
    static partial class Program
    {
        #region tools
#if SINGLE_INSTANCE
        static Mutex singleInstance;
        static readonly string mutexHandle = "$safeprojectname$-$guid2$";

        static bool ShouldRun()
        {
            bool created = false;
            singleInstance = new Mutex(true, mutexHandle, out created);

            return created;
        }
#endif
        public static TraceSource __dataSource = new TraceSource("DataSource");
        public static TraceSource __errorSource = new TraceSource("ErrorSource");
        public static void LogError(object o)
        {
            try
            {
                var entry = new StringBuilder();
                entry = entry.Append(string.Format("[{0:yyyy-MM-dd HH:mm:ss}] ", DateTime.Now));
                entry = entry.Append((o ?? string.Empty).ToString());

                __errorSource.WriteLine(entry.ToString().Trim());
            }
            catch { }
        }
        public static void LogError(string format, object o, params object[] rest)
        {
            try
            {
                var all = new List<object>();
                all.Add(o);
                all.AddRange(rest);

                var entry = new StringBuilder();
                entry = entry.Append(string.Format("[{0:yyyy-MM-dd HH:mm:ss}] ", DateTime.Now));
                entry = entry.Append(string.Format(format, all.ToArray()));

                __errorSource.WriteLine(entry.ToString().Trim());
            }
            catch { }
        }
        public static void LogData(object o)
        {
            try
            {
                var entry = new StringBuilder();
                entry = entry.AppendLine(string.Format("[START [{0:yyyy-MM-dd HH:mm:ss}] ", DateTime.Now));
                entry = entry.AppendLine((o ?? string.Empty).ToString());
                entry = entry.AppendLine("END]");

                __dataSource.WriteLine(entry.ToString().Trim());
            }
            catch { }
        }
        public static void Log(object o)
        {
            try
            {
                var entry = new StringBuilder();
                entry = entry.Append(string.Format("[{0:yyyy-MM-dd HH:mm:ss}] ", DateTime.Now));
                entry = entry.Append((o ?? string.Empty).ToString());

                Trace.WriteLine(entry.ToString().Trim());
            }
            catch { }
        }
        public static void Log(string format, object o, params object[] rest)
        {
            try
            {
                var all = new List<object>();
                all.Add(o);
                all.AddRange(rest);

                var entry = new StringBuilder();
                entry = entry.Append(string.Format("[{0:yyyy-MM-dd HH:mm:ss}] ", DateTime.Now));
                entry = entry.Append(string.Format(format, all.ToArray()));

                Trace.WriteLine(entry.ToString().Trim());
            }
            catch { }
        }
        #endregion

        #region static constructor
        static Program()
        {
            Trace.WriteLine(new string('-', 80));
        }
        #endregion

        static void Main(string[] args)
        {
#if SINGLE_INSTANCE
            if (!ShouldRun()) return;             
#endif
            // your app

            Console.WriteLine("press any key to continue ...");
            Console.ReadKey();
        }
    }
}
